﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Basic4Net.Entities;

namespace Basic4Net.BusinessRules
{
    public class ListPermit
    {
        private int _teacherid;
        private string _typename;
        private string _teachcode;
        private string _teachname;
        private int _ftype1;
        private int _ftype2;
        private int _ftype3;
        private int _ftype4;
        private int _ftype5;
        private int _ftype6;
        private int _super;

        public int TeacherID
        {
            get { return _teacherid; }
            set { _teacherid = value; }
        }

        public string TypeName
        {
            get { return _typename; }
            set { _typename = value; }
        }

        public string TeachCode
        {
            get { return _teachcode; }
            set { _teachcode = value; }
        }

        public string TeachName
        {
            get { return _teachname; }
            set { _teachname = value; }
        }

        public int FType1
        {
            get { return _ftype1; }
            set { _ftype1 = value; }
        }

        public int FType2
        {
            get { return _ftype2; }
            set { _ftype2 = value; }
        }

        public int FType3
        {
            get { return _ftype3; }
            set { _ftype3 = value; }
        }

        public int FType4
        {
            get { return _ftype4; }
            set { _ftype4 = value; }
        }

        public int FType5
        {
            get { return _ftype5; }
            set { _ftype5 = value; }
        }

        public int FType6
        {
            get { return _ftype6; }
            set { _ftype6 = value; }
        }

        public int Super
        {
            get { return _super; }
            set { _super = value; }
        }
    }
    public class Permit : BaseBR
    {
        public bool Add(Entities.t_Permit model,ref string msg)
        {
            bool result = true;
            using (DBEntities db = new DBEntities())
            {
                db.t_Permit.AddObject(model);
                try
                {
                    db.SaveChanges();
                }
                catch
                {
                    msg = "操作失败！";
                    result = false;
                }
                return result;
            }
        }

        public bool Delete(t_Permit model, ref string msg)
        {
            using (DBEntities db = new DBEntities())
            {
                t_Permit model2 = db.t_Permit.FirstOrDefault(t => t.TeacherID == model.TeacherID && t.FType == model.FType && t.Super == model.Super);
                db.DeleteObject(model2);
                try
                {
                    db.SaveChanges();
                    return true;
                }
                catch
                {
                    msg = "操作失败";
                    return false;
                }
            }
        }

        public IList<ListPermit> ListTeacher(int FType, string TeachCode, string TeachName)
        {
            using (DBEntities db = new DBEntities())
            {
                var obj0 = (from b in db.TEACHERINFO
                            join c in db.t_PostRelation on b.RID equals c.TeacherID into temp1
                            from d in temp1.DefaultIfEmpty()
                            join a in db.t_Post on d.PostID equals a.RID into temp2
                            from e in temp2.DefaultIfEmpty()
                            join f in db.t_Permit on b.RID equals f.TeacherID into temp3
                            from g in temp3.DefaultIfEmpty()
                            where (FType == -1 ? true : (FType == 0 ? e.PostName == null : e.RID == FType))
                            && (String.IsNullOrEmpty(TeachCode.Trim()) ? true : b.TEACHCODE.StartsWith(TeachCode.Trim()))
                            && (String.IsNullOrEmpty(TeachName.Trim()) ? true : b.TEACHNAME.StartsWith(TeachName.Trim()))
                            && (g.FType == null || g.FType == 0)
                            select new
                            {
                                TeacherID = b.RID,
                                TypeName = e.PostName,
                                TeachCode = b.TEACHCODE,
                                TeachName = b.TEACHNAME,
                                FType1 = 0,
                                FType2 = 0,
                                FType3 = 0,
                                FType4 = 0,
                                FType5 = 0,
                                FType6 = 0,
                                Super = 0
                            });
                var obj1 = (from b in db.TEACHERINFO
                            join c in db.t_PostRelation on b.RID equals c.TeacherID into temp1
                            from d in temp1.DefaultIfEmpty()
                            join a in db.t_Post on d.PostID equals a.RID into temp2
                            from e in temp2.DefaultIfEmpty()
                            join f in db.t_Permit on b.RID equals f.TeacherID into temp3
                            from g in temp3.DefaultIfEmpty()
                            where (FType == -1 ? true : (FType == 0 ? e.PostName == null : e.RID == FType))
                            && (String.IsNullOrEmpty(TeachCode.Trim()) ? true : b.TEACHCODE.StartsWith(TeachCode.Trim()))
                            && (String.IsNullOrEmpty(TeachName.Trim()) ? true : b.TEACHNAME.StartsWith(TeachName.Trim()))
                            && g.FType == 1
                            select new
                            {
                                TeacherID = b.RID,
                                TypeName = e.PostName,
                                TeachCode = b.TEACHCODE,
                                TeachName = b.TEACHNAME,
                                FType1 = 1,
                                FType2 = 0,
                                FType3 = 0,
                                FType4 = 0,
                                FType5 = 0,
                                FType6 = 0,
                                Super = 0
                            });
                var obj2 = (from b in db.TEACHERINFO
                            join c in db.t_PostRelation on b.RID equals c.TeacherID into temp1
                            from d in temp1.DefaultIfEmpty()
                            join a in db.t_Post on d.PostID equals a.RID into temp2
                            from e in temp2.DefaultIfEmpty()
                            join f in db.t_Permit on b.RID equals f.TeacherID into temp3
                            from g in temp3.DefaultIfEmpty()
                            where (FType == -1 ? true : (FType == 0 ? e.PostName == null : e.RID == FType))
                            && (String.IsNullOrEmpty(TeachCode.Trim()) ? true : b.TEACHCODE.StartsWith(TeachCode.Trim()))
                            && (String.IsNullOrEmpty(TeachName.Trim()) ? true : b.TEACHNAME.StartsWith(TeachName.Trim()))
                            && g.FType == 2
                            select new
                            {
                                TeacherID = b.RID,
                                TypeName = e.PostName,
                                TeachCode = b.TEACHCODE,
                                TeachName = b.TEACHNAME,
                                FType1 = 0,
                                FType2 = 1,
                                FType3 = 0,
                                FType4 = 0,
                                FType5 = 0,
                                FType6 = 0,
                                Super = 0
                            });
                var obj3 = (from b in db.TEACHERINFO
                            join c in db.t_PostRelation on b.RID equals c.TeacherID into temp1
                            from d in temp1.DefaultIfEmpty()
                            join a in db.t_Post on d.PostID equals a.RID into temp2
                            from e in temp2.DefaultIfEmpty()
                            join f in db.t_Permit on b.RID equals f.TeacherID into temp3
                            from g in temp3.DefaultIfEmpty()
                            where (FType == -1 ? true : (FType == 0 ? e.PostName == null : e.RID == FType))
                            && (String.IsNullOrEmpty(TeachCode.Trim()) ? true : b.TEACHCODE.StartsWith(TeachCode.Trim()))
                            && (String.IsNullOrEmpty(TeachName.Trim()) ? true : b.TEACHNAME.StartsWith(TeachName.Trim()))
                            && g.FType == 3
                            select new
                            {
                                TeacherID = b.RID,
                                TypeName = e.PostName,
                                TeachCode = b.TEACHCODE,
                                TeachName = b.TEACHNAME,
                                FType1 = 0,
                                FType2 = 0,
                                FType3 = 1,
                                FType4 = 0,
                                FType5 = 0,
                                FType6 = 0,
                                Super = 0
                            });
                var obj4 = (from b in db.TEACHERINFO
                            join c in db.t_PostRelation on b.RID equals c.TeacherID into temp1
                            from d in temp1.DefaultIfEmpty()
                            join a in db.t_Post on d.PostID equals a.RID into temp2
                            from e in temp2.DefaultIfEmpty()
                            join f in db.t_Permit on b.RID equals f.TeacherID into temp3
                            from g in temp3.DefaultIfEmpty()
                            where (FType == -1 ? true : (FType == 0 ? e.PostName == null : e.RID == FType))
                            && (String.IsNullOrEmpty(TeachCode.Trim()) ? true : b.TEACHCODE.StartsWith(TeachCode.Trim()))
                            && (String.IsNullOrEmpty(TeachName.Trim()) ? true : b.TEACHNAME.StartsWith(TeachName.Trim()))
                            && g.FType == 4
                            select new
                            {
                                TeacherID = b.RID,
                                TypeName = e.PostName,
                                TeachCode = b.TEACHCODE,
                                TeachName = b.TEACHNAME,
                                FType1 = 0,
                                FType2 = 0,
                                FType3 = 0,
                                FType4 = 1,
                                FType5 = 0,
                                FType6 = 0,
                                Super = 0
                            });
                var obj5 = (from b in db.TEACHERINFO
                            join c in db.t_PostRelation on b.RID equals c.TeacherID into temp1
                            from d in temp1.DefaultIfEmpty()
                            join a in db.t_Post on d.PostID equals a.RID into temp2
                            from e in temp2.DefaultIfEmpty()
                            join f in db.t_Permit on b.RID equals f.TeacherID into temp3
                            from g in temp3.DefaultIfEmpty()
                            where (FType == -1 ? true : (FType == 0 ? e.PostName == null : e.RID == FType))
                            && (String.IsNullOrEmpty(TeachCode.Trim()) ? true : b.TEACHCODE.StartsWith(TeachCode.Trim()))
                            && (String.IsNullOrEmpty(TeachName.Trim()) ? true : b.TEACHNAME.StartsWith(TeachName.Trim()))
                            && g.FType == 5
                            select new
                            {
                                TeacherID = b.RID,
                                TypeName = e.PostName,
                                TeachCode = b.TEACHCODE,
                                TeachName = b.TEACHNAME,
                                FType1 = 0,
                                FType2 = 0,
                                FType3 = 0,
                                FType4 = 0,
                                FType5 = 1,
                                FType6 = 0,
                                Super = 0
                            });
                var obj6 = (from b in db.TEACHERINFO
                            join c in db.t_PostRelation on b.RID equals c.TeacherID into temp1
                            from d in temp1.DefaultIfEmpty()
                            join a in db.t_Post on d.PostID equals a.RID into temp2
                            from e in temp2.DefaultIfEmpty()
                            join f in db.t_Permit on b.RID equals f.TeacherID into temp3
                            from g in temp3.DefaultIfEmpty()
                            where (FType == -1 ? true : (FType == 0 ? e.PostName == null : e.RID == FType))
                            && (String.IsNullOrEmpty(TeachCode.Trim()) ? true : b.TEACHCODE.StartsWith(TeachCode.Trim()))
                            && (String.IsNullOrEmpty(TeachName.Trim()) ? true : b.TEACHNAME.StartsWith(TeachName.Trim()))
                            && g.FType == 6
                            select new
                            {
                                TeacherID = b.RID,
                                TypeName = e.PostName,
                                TeachCode = b.TEACHCODE,
                                TeachName = b.TEACHNAME,
                                FType1 = 0,
                                FType2 = 0,
                                FType3 = 0,
                                FType4 = 0,
                                FType5 = 0,
                                FType6 = 1,
                                Super = 0
                            });
                var obj7 = (from b in db.TEACHERINFO
                            join c in db.t_PostRelation on b.RID equals c.TeacherID into temp1
                            from d in temp1.DefaultIfEmpty()
                            join a in db.t_Post on d.PostID equals a.RID into temp2
                            from e in temp2.DefaultIfEmpty()
                            join f in db.t_Permit on b.RID equals f.TeacherID into temp3
                            from g in temp3.DefaultIfEmpty()
                            where (FType == -1 ? true : (FType == 0 ? e.PostName == null : e.RID == FType))
                            && (String.IsNullOrEmpty(TeachCode.Trim()) ? true : b.TEACHCODE.StartsWith(TeachCode.Trim()))
                            && (String.IsNullOrEmpty(TeachName.Trim()) ? true : b.TEACHNAME.StartsWith(TeachName.Trim()))
                            && g.Super == 1
                            select new
                            {
                                TeacherID = b.RID,
                                TypeName = e.PostName,
                                TeachCode = b.TEACHCODE,
                                TeachName = b.TEACHNAME,
                                FType1 = 0,
                                FType2 = 0,
                                FType3 = 0,
                                FType4 = 0,
                                FType5 = 0,
                                FType6 = 0,
                                Super = 1
                            });
                var obj8 = obj0.Union(obj1).Union(obj2).Union(obj3).Union(obj4).Union(obj5).Union(obj6).Union(obj7);
                var obj = (from t in obj8
                           where t.TeachCode != "admin"
                           group t by new
                           {
                               t.TeacherID,
                               t.TypeName,
                               t.TeachCode,
                               t.TeachName
                           } into temp
                           select new ListPermit
                           {
                               TeacherID = temp.Key.TeacherID,
                               TypeName = temp.Key.TypeName,
                               TeachCode = temp.Key.TeachCode,
                               TeachName = temp.Key.TeachName,
                               FType1 = temp.Sum(z => z.FType1),
                               FType2 = temp.Sum(z => z.FType2),
                               FType3 = temp.Sum(z => z.FType3),
                               FType4 = temp.Sum(z => z.FType4),
                               FType5 = temp.Sum(z => z.FType5),
                               FType6 = temp.Sum(z => z.FType6),
                               Super = (int)temp.Sum(z => z.Super)
                           });
                return obj.ToList();
            }
        }
    }
}
